<?php
    $libraryCardNo = $_GET['libraryCardNo'];
    $libraryNo = $_COOKIE['libraryNo'];
    $callNo = $_POST['callNo'];
    
    require('db_connection.php');
    $sql = "SELECT * FROM tblclient WHERE libraryCardNo = '$libraryCardNo' and libraryNo='$libraryNo'";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result))
    {
        $userID = $row['userID'];
    }
    
    $sql2 = "SELECT * FROM tblcollection WHERE accessionNo = '$callNo'";
    $result2 = mysql_query($sql2);
    while ($row = mysql_fetch_array($result2))
    {
        $materialIDtmp = $row['materialID'];
        $sql4 = "SELECT * FROM tblcirculation WHERE userID='$userID'";
        $result4 = mysql_query($sql4);
        while ($row = mysql_fetch_array($result4))
        {
          $materialID1 = $row['materialID'];
          if($materialIDtmp==$materialID1)
          {
            $materialID = $materialID1;
          }
        }
            
    }
    
    $sql3 = "DELETE FROM tblcirculation WHERE materialID = '$materialID' AND userID = '$userID' AND libraryNo='$libraryNo' AND status='reserved' ";
    
    $result3 = mysql_query($sql3);
    if($result3)
    {
        echo"<script> alert('Reservation Canceled!');</script>";
        echo"'<script>document.location.href='transactions.php?libraryCardNo=$libraryCardNo'</script>'";
    }
    else
    {
      echo "Delete failed";
    }

?>
